package com.example.event_resource_java.authority.controller.EventResourceContoller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.event_resource_java.authority.dao.EventResourceAllocationsDO;
import com.example.event_resource_java.authority.dao.ModuleDao;
import com.example.event_resource_java.authority.mapper.ResourceMapper;
import com.example.event_resource_java.authority.server.ResourceServer;
import com.example.event_resource_java.authority.server.UserServer;
import com.example.event_resource_java.commons.annotation.Anonymous;
import com.example.event_resource_java.commons.domain.UserDO;
import com.example.event_resource_java.commons.enums.Constants;
import com.example.event_resource_java.commons.model.CaseBody;
import com.example.event_resource_java.commons.model.PracticeRecordBody;
import com.example.event_resource_java.commons.model.TheoreticalBody;
import com.example.event_resource_java.commons.util.ApiResult;
import com.example.event_resource_java.commons.util.JWTUtil;
import com.example.event_resource_java.commons.vo.*;
import com.example.event_resource_java.starinside.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HomeController
 * @Description: 首页资源
 * @Author: ak
 * @CreateDate: 2024/4/11 14:42
 * @UpdateUser:
 * @UpdateDate: 2024/4/11 14:42
 * @UpdateRemark:
 * @Version: 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/resource")
public class ResourceController {

    @Resource
    private ResourceServer resourceServer;
    @Resource
    private UserServer userServer;
    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 首页所有赛项资源名称
     *
     * @return
     */
    @GetMapping("/titleList")
    public ApiResult getAllocationTitleList() {
        TitleListVO allocationTitleList = resourceServer.getAllocationTitleList();
        return ApiResult.success(allocationTitleList);
    }


    @GetMapping("/headLineTitle")
    public ApiResult getHeadLineTitle() {
        List<String> headLineTitle = resourceServer.getHeadLineTitle();
        return ApiResult.success(headLineTitle);
    }

    /**
     * @auther: ak
     * @Description 首页全部资源
     * @param:
     * @return:
     * @date: 2024/4/11 14:55
     */
    @GetMapping("/list")
    public Object homeResource() {
        List<HomeResourceVO> list = resourceServer.getHomeResourceList();
//        RedisUtil.get(Constants.LOGIN_TOKEN_KEY + request.getHeader("Authorization"));
        return ApiResult.success(list);
    }

    /**
     * @auther: ak
     * @Description 赛项资源首页参数查询
     * @param:
     * @return:
     * @date: 2024/4/13 9:19
     */
    @GetMapping("/getMatchResource")
    public ApiResult getMatchResource(@RequestParam("eventName") String eventName, @RequestParam("eveMoId") String eveMoId,
                                      @RequestParam("type") Integer eventType, @RequestParam("time") String time,
                                      @RequestParam("grade") Integer grade,
                                      @RequestParam("startPage") Integer pageNum, @RequestParam("endPage") Integer pageSize) {
        if (pageNum == null && pageSize == null) {
            return ApiResult.fail();
        }
        PageResult matchResourceList = resourceServer.getMatchResourceList(eventName, eventType, time, grade, Long.valueOf(eveMoId), pageNum, pageSize);
        return ApiResult.success(matchResourceList);
    }

    /**
     * @auther: ak
     * @Description 首页栏资源名称
     * @param:
     * @return:
     * @date: 2024/4/12 10:56
     */
    @GetMapping("/getModuleList")
    public ApiResult<List<ModuleDao>> getModuleList() {
        return ApiResult.success(resourceServer.getModuleList());
    }

    /**
     * @auther: ak
     * @Description 赛项资源查询参数
     * @param:
     * @return:
     * @date: 2024/4/13 13:38
     */
    @GetMapping("/getMatchRequestParam")
    public ApiResult<MatchRequestVO> getMatchRequestParam(@RequestParam("eveMoId") String eveMoId) {
        return ApiResult.success(resourceServer.getMatchRequestParam(eveMoId));
    }


    /**
     * @auther: ak
     * @Description //赛项资源详情
     * @param:
     * @return:
     * @date: 2024/4/13 13:37
     */
    @GetMapping("/getResourceDetails")
    public ApiResult getResourceDetails(@RequestParam("id") Long id) {
        ApiResult result = resourceServer.getMatchResourceDetails(id);
        return result;
    }

    /**
     * @auther: ak
     * @Description // 资源子节点
     * @param:
     * @return:
     * @date: 2024/4/13 13:37
     */
    @GetMapping("/getChildren")
    public ApiResult<Map<String, List<EventResourceAllocationsDO>>> getChildren(@RequestParam("parentId") String parentId) {
        Map<String, List<EventResourceAllocationsDO>> children = resourceServer.getChildren(Long.valueOf(parentId));
        return ApiResult.success(children);
    }

    /**
     * 案例分析题新增 / 修改接口
     *
     * @param caseBody
     * @return
     */
    @PostMapping("/insertUpdateCase")
    public ApiResult insetCase(@RequestBody CaseBody caseBody) {
        Long userId = JWTUtil.getUser().getId();
        QueryWrapper<UserDO> userDOQueryWrapper = new QueryWrapper<>();
        userDOQueryWrapper.eq("id", userId);
        UserDO one = userServer.getOne(userDOQueryWrapper);
        if (one.getType() == 1) {
            return ApiResult.success(true);
        }

        boolean success = resourceServer.saveOrUpdateCaseRecord(caseBody);
        return ApiResult.success(success);
    }

    /**
     * @auther: ak
     * @Description 开始考试 根据模板设置获取考试题目 同时存储一份练习记录
     * @param:
     * @return:
     * @date: 2024/4/28 16:52
     */
    @PostMapping("" +
            "/getExamination")
    public ApiResult getExamination(@RequestBody TheoreticalBody theoreticalBody) throws ParseException {
        // TODO
        theoreticalBody.setItemBankType(0);
        ApiResult examination = resourceServer.getExamination2(theoreticalBody);
        return examination;
    }

    /**
     * @auther: ak
     * @Description
     * @param: 更新做题 暂存
     * @return:
     * @date: 2024/5/18 16:07
     */
    @PostMapping("/update")
    public ApiResult update(@RequestBody PracticeRecordBody practiceRecordBody) {
        // 老师不记录
        Long userId = JWTUtil.getUser().getId();
        QueryWrapper<UserDO> userDOQueryWrapper = new QueryWrapper<>();
        userDOQueryWrapper.eq("id", userId);
        UserDO one = userServer.getOne(userDOQueryWrapper);
        if (one.getType() == 1) {
            return ApiResult.success(resourceServer.teacherExamPaperDetail(practiceRecordBody));
        }

        return ApiResult.success(resourceServer.update(practiceRecordBody));
    }


    /**
     * @auther: ak
     * @Description 在线练习交卷 更新练习记录 返回试卷做题详情
     * @param:
     * @return:
     * @date: 2024/4/29 16:44
     */
    @PostMapping("/submitPaper")
    public ApiResult submitPaper(@RequestBody PracticeRecordBody practiceRecordBody) throws ParseException {
        // TODO 老师不记录
        Long userId = JWTUtil.getUser().getId();
        QueryWrapper<UserDO> userDOQueryWrapper = new QueryWrapper<>();
        userDOQueryWrapper.eq("id", userId);
        UserDO one = userServer.getOne(userDOQueryWrapper);
        if (one.getType() == 1) {
            return ApiResult.success(true);
        }

        return resourceServer.submitPaper2(practiceRecordBody);
    }

    /**
     * @auther: ak
     * @Description 继续答题
     * @param:
     * @return:
     * @date: 2024/5/8 11:17
     */
    @GetMapping("/getContinueExam")
    public ApiResult continueExam(@RequestParam("practiceRecordId") Long practiceRecordId) {
        return ApiResult.success(resourceServer.continueExam(practiceRecordId));
    }

    /**
     * @auther: ak
     * @Description 重新练习
     * @param:
     * @return:
     * @date: 2024/5/10 14:02
     */
    @GetMapping("/redoPractice")
    public ApiResult redoPractice(@RequestParam("practiceRecordId") Long practiceRecordId) {
        PaperDetailVo paperDetailVo = resourceServer.redoPractice(practiceRecordId);
        if (ObjectUtil.isNull(paperDetailVo)) {
            return ApiResult.fail("该练习记录还未做完，不能重新练习!");
        }
        return ApiResult.success(paperDetailVo);
    }

    // 用户id 试卷id 状态 判断是否有未做完记录
    @CrossOrigin
    @GetMapping("/getRedoJudge")
    public ApiResult getRedoJudge(@RequestParam("templateId") String templateId) {
        // 用户id 试卷id 状态 判断是否有未做完记录
        Long practiceId = resourceMapper.isExistNotDoneRecord(JWTUtil.getUser().getId(), Long.parseLong(templateId));
        if (ObjectUtil.isNotNull(practiceId)) {
            return ApiResult.success("0", "0");
        }
        return ApiResult.success("1", "1");
    }

    /**
     * @auther: ak
     * @Description 首页搜索
     * @param:
     * @return:
     * @date: 2024/4/13 15:47
     */
    @GetMapping("/homeSearch")
    public ApiResult homeSearch(@RequestParam("searchValue") String searchValue,
                                @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        return ApiResult.success(resourceServer.homeSearch(searchValue, pageNum, pageSize));
    }

//    @GetMapping("/cleanPracticeRecord")


}
